<?php

namespace App\Models;

class Stores extends Base
{
    protected $table = 'stores';

    const STATUS_1 = 1;
    const STATUS_2 = 2;
    const STATUS_3 = 3;
    const STATUS_4 = 4;

    const STATUS_TEXT = [
        self::STATUS_1 => '<span class="el-tag">正常</span>',
        self::STATUS_2 => '<span class="el-tag el-tag--danger">待审核</span>',
        self::STATUS_3 => '<span class="el-tag el-tag--info">未通过</span>',
        self::STATUS_4 => '<span class="el-tag el-tag--info">关店</span>',
    ];

    const RECOMMEND_1 = 1;
    const RECOMMEND_2 = 2;

    const RECOMMEND_TEXT = [
        self::RECOMMEND_1 => '<span class="el-tag">否</span>',
        self::RECOMMEND_2 => '<span class="el-tag  el-tag--danger">是</span>',
    ];

    protected $fillable = [
        'id','user_id','seller_name','store_name','logo','image','cate_pid','cate_id','phone','service_area','service_scope',
        'area_info','website','description','workingtime','view','collections','call','service','praise','credit',
        'sales','partners','recommend','hot','quality','status','created_at','updated_at'
    ];

    protected $casts = ['image' => 'array', 'partners' => 'array'];

    public function cate_pid(){//一级分类
        return $this->belongsTo('App\Models\Categorys', 'cate_pid', 'id');
    }
    public function cate_id(){//二级分类
        return $this->belongsTo('App\Models\Categorys', 'cate_id', 'id');
    }

    /**
     * 获取列表
     * @param array $params 条件参数
     * @param array $fields 查询的字段
     * @param int $limit 分页数量
     * @return mixed
     */
    protected function getList($params = [], $fields = ['*'], $limit = 10){
        $lists = $this->select($fields)
            ->where(function ($query) use ($params) {
                if(isset($params['kwd']) && $params['kwd']) {
                    $query ->where('store_name','like','%' . $params['kwd'] . '%');
                }
                if(isset($params['status']) && $params['status']) {
                    $query ->where('status',$params['status']);
                }
                if(isset($params['recommend']) && $params['recommend']) {
                    $query ->where('recommend',$params['recommend']);
                }
                if(isset($params['cid']) && $params['cid']) {
                    $cid = $params['cid'];
                    $query ->whereRaw("FIND_IN_SET($cid,cate_id)")->orwhereRaw("FIND_IN_SET($cid,cate_pid)");
                }
                if(isset($params['start']) && $params['start']) {
                    $query ->whereDate('created_at','>=',$params['start']);
                }
                if(isset($params['end']) && $params['end']) {
                    $query ->whereDate('created_at','<=',$params['end']);
                }
            })->where('status','<>',-1)
            ->when($params, function ($query) use($params){
                if(isset($params['sort']) && $params['sort']) {
                    return $query->orderBy('credit',$params['sort']);
                }
            })->orderBy('created_at', 'desc')
            ->paginate($limit);

        $this->stateToText($lists,[
            'status'=> self::STATUS_TEXT,
            'recommend'   => self::RECOMMEND_TEXT,
        ]);
        return $lists;
    }

    /**
     * 所有数据
     * @param array $params 条件参数
     * @param array $fields 查询的字段
     * @param int $limit 分页数量
     * @return mixed
     */
    protected function allLists($params = [], $fields = ['*'], $page = '', $limit = 10, $sort = 'created_at', $orderby = 'desc'){
        $params['status'] = 1;
        if(isset($page)) {
            $lists = $this->select($fields)
                ->where(function ($query) use ($params) {
                    if(isset($params['id']) && $params['id']) {
                        $query ->where('id',$params['id']);
                    }
                    if(isset($params['status']) && $params['status']) {
                        $query ->where('status',$params['status']);
                    }
                    if(isset($params['service_area']) && $params['service_area']) {
                        $query ->where('service_area',$params['service_area']);
                    }
                    if(isset($params['recommend']) && $params['recommend']) {
                        $query ->where('recommend',$params['recommend']);
                    }
                    if(isset($params['cid']) && $params['cid']) {
                        $cid = $params['cid'];
                        $query ->whereRaw("FIND_IN_SET($cid,cate_pid)");
                    }
                    if(isset($params['cid_id']) && $params['cid_id']) {
                        $cid_id = $params['cid_id'];
                        $query ->whereRaw("FIND_IN_SET($cid_id,cate_id)");
                    }
                    if(isset($params['keyword']) && $params['keyword']) {
                        $keyword = $params['keyword'];
                        $query ->where("store_name", "like", "%$keyword%")->orWhere("seller_name", "like", "%$keyword%");
                    }
                })->where('status','<>',-1)
                ->orderBy($sort, $orderby)->paginate($limit, null, null, $page);
        }else {
            $lists = $this->select($fields)
                ->where(function ($query) use ($params) {
                    if(isset($params['status']) && $params['status']) {
                        $query ->where('status',$params['status']);
                    }
                    if(isset($params['recommend']) && $params['recommend']) {
                        $query ->where('recommend',$params['recommend']);
                    }
                    if(isset($params['cid']) && $params['cid']) {
                        $cid = $params['cid'];
                        $query ->whereRaw("FIND_IN_SET($cid,cate_id)")->orwhereRaw("FIND_IN_SET($cid,cate_pid)");
                    }
                })->where('status','<>',-1)
                ->orderBy('created_at', 'desc')->limit($limit)->get();
        }
        return $lists;
    }

    /**
     * 手机端获取供应列表-多选
     * @param array $params 条件参数
     * @param array $fields 查询的字段
     * @param int $limit 分页数量
     * @return mixed
     */
    protected function webLists($params = [], $fields = ['*'], $page = '', $limit = 10, $sort = 'created_at', $orderby = 'desc'){
        $params['status'] = 1;
        $lists = $this->select($fields)
            ->where(function ($query) use ($params) {
                if(isset($params['id']) && $params['id']) {
                    $query ->where('id',$params['id']);
                }
                if(isset($params['status']) && $params['status']) {
                    $query ->where('status',$params['status']);
                }
                if(isset($params['service_area']) && $params['service_area']) {
                    $query ->where('service_area',$params['service_area']);
                }
                if(isset($params['recommend']) && $params['recommend']) {
                    $query ->where('recommend',$params['recommend']);
                }
                if(isset($params['cid_id']) && $params['cid_id']) {
                    $cid_id = explode(",", $params['cid_id']);
                    $map = "(";
                    foreach ($cid_id as $v){
                        $map .= "FIND_IN_SET($v,cate_id) OR ";
                    }
                    $map = substr($map,0,strlen($map)-3);
                    $map .=")";
                    $query ->whereRaw($map);
                }
                if(isset($params['keyword']) && $params['keyword']) {
                    $keyword = $params['keyword'];
                    $query ->where("store_name", "like", "%$keyword%")->orWhere("seller_name", "like", "%$keyword%");
                }
            })->where('status','<>',-1)
            ->orderBy($sort, $orderby)->paginate($limit, null, null, $page);
        return $lists;
    }

    /**
     * 根据id查询一条供应商
     * @param array $params 条件参数
     * @param array $fields 查询的字段
     * @param int $limit 分页数量
     * @return mixed
     */
    protected function details($id, $fields = ['*']){
        return $this->select($fields)->where($id)->where('status','<>',-1)->first();
    }

}
